home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 98 / CD-ROM 98.iso / infantil / tuxmath / tuxmath-2001.09.07-win32-installer.exe / src / options.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-07  |  3.5 KB  |  194 lines

  1. /*
  2.   options.c
  3.  
  4.   For TuxMath
  5.   The options screen loop.
  6.  
  7.   by Bill Kendrick
  8.   bill@newbreedsoftware.com
  9.   http://www.newbreedsoftware.com/
  10.  
  11.  
  12.   Part of "Tux4Kids" Project
  13.   http://www.tux4kids.org/
  14.       
  15.   August 26, 2001 - September 6, 2001
  16. */
  17.  
  18.  
  19. #define OPTIONS_DEVEL
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <SDL.h>
  25. #include "options.h"
  26. #include "images.h"
  27. #include "setup.h"
  28. #include "sounds.h"
  29.  
  30. #ifdef OPTIONS_DEVEL
  31. #include <SDL_image.h>
  32. #endif
  33.  
  34.  
  35. int options(void)
  36. {
  37.   int opt, old_opt, done, quit, img, blinking;
  38.   SDL_Rect dest;
  39.   SDL_Event event;
  40.   Uint32 last_time, now_time;
  41.   SDLKey key;
  42.   SDL_Surface * bkgd;
  43.   
  44.   
  45.   /* Clear window: */
  46.   
  47.   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  48.  
  49.  
  50. #ifdef OPTIONS_DEVEL
  51.   bkgd = IMG_Load(DATA_PREFIX "/images/options-devel.jpg");
  52.   if (bkgd == NULL)
  53.   {
  54.     fprintf(stderr,
  55.         "\nWarning: Could not load:\n"
  56.         DATA_PREFIX "/images/options-devel.jpg\n"
  57.         "The Simple DirectMedia error that occured was: %s\n",
  58.         SDL_GetError());
  59.  
  60.     return 0;
  61.   }
  62.  
  63.   SDL_BlitSurface(bkgd, NULL, screen, NULL);
  64.   SDL_Flip(screen);
  65. #endif
  66.   
  67.   
  68.   /* --- MAIN OPTIONS SCREEN LOOP: --- */
  69.  
  70.   blinking = 0;
  71.   opt = 0;
  72.   done = 0;
  73.   quit = 0;
  74.   
  75.   do
  76.     {
  77.       last_time = SDL_GetTicks();
  78.       old_opt = opt;
  79.       
  80.       
  81.       /* Handle any incoming events: */
  82.       
  83.       while (SDL_PollEvent(&event) > 0)
  84.     {
  85.       if (event.type == SDL_QUIT)
  86.         {
  87.           /* Window close event - quit! */
  88.           
  89.           quit = 1;
  90.           done = 1;
  91.         }
  92.       else if (event.type == SDL_KEYDOWN)
  93.         {
  94.           key = event.key.keysym.sym;
  95.           
  96.           if (key == SDLK_ESCAPE)
  97.         {
  98.           /* Escape key - quit! */
  99.           
  100.           done = 1;
  101.         }
  102.           else if (key == SDLK_DOWN)
  103.         {
  104.           opt++;
  105.           
  106.           if (opt >= NUM_OPTS)
  107.             opt = NUM_OPTS - 1;
  108.         }
  109.           else if (key == SDLK_UP)
  110.         {
  111.           opt--;
  112.           
  113.           if (opt < 0)
  114.             opt = 0;
  115.         }
  116.           else if (key == SDLK_RETURN)
  117.         {
  118.           done = 1;
  119.         }
  120.         }
  121.     }
  122.       
  123.       
  124.       /* Erase Tux (cursor) */
  125.       
  126.       if (opt != old_opt)
  127.     {
  128.       blinking = 0;
  129.       
  130.       
  131.       dest.x = 32;
  132.       dest.y = (images[IMG_TITLE]->h + 8 + 
  133.             (old_opt * images[IMG_TUX_HELMET1]->h));
  134.       dest.w = images[IMG_TUX_HELMET1]->w;
  135.       dest.h = images[IMG_TUX_HELMET1]->h;
  136.       
  137.       SDL_FillRect(screen, &dest,
  138.                SDL_MapRGB(screen->format, 200, 232, 255));
  139.     }
  140.       
  141.       
  142.       /* Handling Tux (cursor) blinking: */
  143.       
  144.       if ((rand() % 50) == 0 && blinking == 0)
  145.     {
  146.       blinking = 6;
  147.     }
  148.       
  149.       if (blinking > 0)
  150.     blinking--;
  151.       
  152.       
  153.       /* Draw Tux (cursor) */
  154.       
  155.       dest.x = 32;
  156.       dest.y = images[IMG_TITLE]->h + 8 + (opt * images[IMG_TUX_HELMET1]->h);
  157.       dest.w = images[IMG_TUX_HELMET1]->w;
  158.       dest.h = images[IMG_TUX_HELMET1]->h;
  159.       
  160.       img = IMG_TUX_HELMET1;
  161.       
  162.       if (blinking >= 4 || (blinking >= 1 && blinking < 2))
  163.     img = IMG_TUX_HELMET2;
  164.       else if (blinking >= 2 && blinking < 4)
  165.     img = IMG_TUX_HELMET3;
  166.      
  167. #ifndef OPTIONS_DEVEL 
  168.       SDL_BlitSurface(images[img], NULL, screen, &dest);
  169.       
  170.       SDL_Flip(screen);
  171. #endif
  172.  
  173.       
  174.       /* Pause (keep frame-rate event) */
  175.       
  176.       now_time = SDL_GetTicks();
  177.       if (now_time < last_time + (1000 / 20))
  178.     {
  179.       SDL_Delay(last_time + (1000 / 20) - now_time);
  180.     }
  181.     }
  182.   while (!done);
  183.  
  184.  
  185. #ifdef OPTIONS_DEVEL
  186.   SDL_FreeSurface(bkgd);
  187. #endif
  188.   
  189.   
  190.   /* Return the chosen command: */
  191.   
  192.   return quit;
  193. }
  194.